package com.example.finalproject;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnTouchListener;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class start2 extends AppCompatActivity implements Runnable {
    private Context context;
    int time = 30;
    int score = 0;
    int score2 = 0;
    ImageView mouse;
    ImageView mouse2;
    TextView ScoreShow;
    TextView TimeShow;
    Handler handler;
    Handler handler2;

    public int[][] position = new int[][]{
            {190, 764}, {525, 764}, {910, 781},
            {128, 1270}, {520, 1270}, {910, 1270},
            {128, 1765}, {550, 1785}, {910, 1795}
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start2);
        context = this;
        ScoreShow = findViewById(R.id.ScoreShow);
        TimeShow = findViewById(R.id.TimeShow);
        TimeShow.setText(String.valueOf(time));
        mouse = findViewById(R.id.imageView);
        mouse2 = findViewById(R.id.imageView2);


        mouse.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.setVisibility(View.INVISIBLE);
                score++;
                score2 = 2 * score;
                ScoreShow.setText(String.valueOf(score2));
                Toast.makeText(start2.this, "打到地鼠了！",
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                int index1 = 0;
                if (msg.what == 0x101) {
                    index1 = msg.arg1;
                    TimeShow.setText(String.valueOf(time));
                    mouse.setX(position[index1][0]);
                    mouse.setY(position[index1][1]);
                    mouse.setVisibility(View.VISIBLE);
                }
                super.handleMessage(msg);
            }

        };
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                int index = 0;
                while (!Thread.currentThread().isInterrupted()) {
                    index = new Random().nextInt(position.length);
                    Message m1 = handler.obtainMessage();
                    m1.what = 0x101;
                    m1.arg1 = index;
                    handler.sendMessage(m1);
                    if(time==0)
                        break;
                    try {
                        Thread.sleep(new Random().nextInt(220) + 220);
                    } catch (InterruptedException e) {
                        e.printStackTrace(); }
                   } }});
        t.start();




        mouse2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.setVisibility(View.INVISIBLE);
                score++;
                score2 = 2 * score;
                ScoreShow.setText(String.valueOf(score2));
                Toast.makeText(start2.this, "Great！",
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        handler2 = new Handler() {
            @Override
            public void handleMessage(Message msg2) {
                int index2 = 0;
                if (msg2.what == 0x101) {
                    index2 = msg2.arg1;
                    TimeShow.setText(String.valueOf(time));
                    mouse2.setX(position[index2][0]);
                    mouse2.setY(position[index2][1]);
                    mouse2.setVisibility(View.VISIBLE);
                }
                super.handleMessage(msg2);
            }

        };
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                int index2 = 0;
                while (!Thread.currentThread().isInterrupted()) {
                    index2 = new Random().nextInt(position.length);
                    Message m2 = handler2.obtainMessage();
                    m2.what = 0x101;
                    m2.arg1 = index2;
                    handler2.sendMessage(m2);
                    if(time==0)
                        break;
                    try {
                        Thread.sleep(new Random().nextInt(220) + 220);
                    } catch (InterruptedException e) {
                        e.printStackTrace(); }
                } }});
        t2.start();

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    time--;
                    if(time==0)
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setTitle("时间到！");
                        builder.setMessage("你的成绩是："+score2);


                        DBManger dbManger = new DBManger(context);
                        HistoryItem historyItem = new HistoryItem();
                        historyItem.setDifficulty("普通");
                        historyItem.setScore(score2);
                        dbManger.add(historyItem);



                        builder.setPositiveButton("退出", new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog,int which) {
                                //处理退出对话框的事项.
                                Button PositiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
                                Intent intent =new Intent(context,MainActivity.class);
                                startActivityForResult(intent,1);
                            } });
                        Looper.prepare();
                        builder.show();
                        Looper.loop();
                        break;}
                    try {      //抛出异常
                        Thread.sleep(new Random().nextInt(1000) + 1000);   //线程睡眠
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }


        });
        t3.start();     //开启线程


    }



    @Override
    public void run() {

    }


    public void btn(View v){
        if(v.getId()==R.id.exit){
            Intent MainActivity =new Intent(this,MainActivity.class);
            startActivityForResult(MainActivity,1); }
    }

}